from os.path import exists
from pickle import dump, load


class CacheData:
    def __init__(self):
        super(CacheData, self).__init__()

    def __icon(self, tab, enable, down, track, bar):
        """ Dinamic tray-icons and tray-tool changing. """
        prefix = f"{('AnimeVost', 'Manga', 'Ranobe')[tab]} tracker"
        bar.hide()
        if enable and not down and not track:
            ico = ('animevost-new', 'mask-0', 'ranobe-0')[tab]
        elif down:
            ico, prefix = 'down', 'Download file..'
            bar.hide() if tab else bar.show()
        elif track:
            ico, prefix = 'track', 'Tracking..'
        else:
            ico = ('animevost', 'mask', 'ranobe')[tab]

        return ico, prefix

    def __notify(self, ico):
        """ Dynamic notify icon changing. """
        return 'notification' if ico == 'checked' else 'notify' \
                              if ico == 'unchecked' else 'bell'

    def getValue(self, tab, enable, down, ico, track, bar):
        """ Getting all data from class `CasheData`. """
        return self.__notify(ico), *self.__icon(tab, enable, down, track, bar)

    def write(self, path, files):
        """ Creating or rewriting cache file. """
        with open(f'{path}/.cache', 'wb') as f:
            dump(files, f)

    def read(self, path):
        """ Getting data from cache file. """
        if exists(f'{path}/.cache'):
            with open(f'{path}/.cache', 'rb') as f:
                return load(f)
        return ()
